﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using EAppointment.DataSets.UserSetTableAdapters;

namespace EAppointment.Models
{
    public class EmailVerfier
    {
        private long ? ID;
        protected string Email;
        protected BaseUserInfo UserInfo;

        public EmailVerfier(string email, BaseUserInfo user)
        {
            Email = email;
            UserInfo = user;
        }

        protected string GenerateCode()
        {
            string tempStr = string.Format("{0}{1}{2}", UserInfo.RegisterDate, UserInfo.Phone, UserInfo.TrueName);
            MD5 md5 = MD5.Create("MD5");
            byte[] bin = Encoding.UTF8.GetBytes(tempStr);
            byte[] hash = md5.ComputeHash(bin);
            string result = BitConverter.ToString(hash);
            return result.Replace("-", "");
        }

        public bool DoVerify()
        {
            if (Email == GenerateCode())
            {
                int? result = 0;
                TB_BaseUserInfoTableAdapter adapter = new TB_BaseUserInfoTableAdapter();
                adapter.ActiveUser(ID, ref result);
                if (result == 1)
                    return true;
                else
                    return false;
            }
            return false;
        }
    }
}